﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Client.Models;
using Newtonsoft.Json;

namespace Client.Base
{
    public partial class FrmProduct : DevExpress.XtraEditors.XtraForm
    {
        public FrmProduct()
        {
            InitializeComponent();
        }
        private int Sid { get; set; }
        
        private int Suid { get; set; }
        
        private string SCName { get; set; }
        
        private string SParentCode { get; set; }
        
        private string SCode { get; set; }
        
        private string SSimpCode { get; set; }
        
        private string SModels { get; set; }
        
        private string SUnit { get; set; }
        
        private string STaxCode { get; set; }
        
        private int SDiscount { get; set; }
        
        private string STaxItem { get; set; }
        
        private string STax { get; set; }
        
        private decimal SPrice { get; set; }
        
        private int STaxMark { get; set; }
        
        private string STaxName { get; set; }
        
        private string SDiscountCate { get; set; }
        /// <summary>
        /// 载入窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmProduct_Load(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 检索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.uom.Token);
                CommodityModel cm = new CommodityModel();
                cm.CName = textEdit1.Text.Trim();
                RetModel rm = Share.PostData("51API/Base/GetCommidityByLikeName", dc, JsonConvert.SerializeObject(cm));
                if (rm.Success)
                {
                    CommodityModel[] rcm = JsonConvert.DeserializeObject<CommodityModel[]>(rm.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("关联id");
                    dt.Columns.Add("商品名称");
                    dt.Columns.Add("编码");
                    dt.Columns.Add("上级编码");
                    dt.Columns.Add("简码");
                    dt.Columns.Add("规格型号");
                    dt.Columns.Add("计量单位");
                    dt.Columns.Add("税收分类编码");
                    dt.Columns.Add("税收分类名称");
                    dt.Columns.Add("优惠政策");
                    dt.Columns.Add("优惠政策类型");
                    dt.Columns.Add("税目");
                    dt.Columns.Add("税率");
                    dt.Columns.Add("单价");
                    dt.Columns.Add("含税标志");
                    for (int i = 0; i < rcm.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = rcm[i].id;
                        dr["uid"] = rcm[i].uid;
                        dr["商品名称"] = rcm[i].CName;
                        dr["编码"] = rcm[i].Code;
                        dr["上级编码"] = rcm[i].ParentCode;
                        dr["简码"] = rcm[i].SimpCode;
                        dr["规格型号"] = rcm[i].Models;
                        dr["计量单位"] = rcm[i].Unit;
                        dr["税收分类编码"] = rcm[i].TaxCode;
                        dr["税收分类名称"] = rcm[i].TaxName;
                        if (rcm[i].Discount == 0)
                            dr["优惠政策"] = "否";
                        else
                            dr["优惠政策"] = "是";
                        dr["优惠政策类型"] = rcm[i].DiscountCate;
                        dr["税目"] = rcm[i].TaxItem;
                        dr["税率"] = rcm[i].Tax;
                        dr["单价"] = rcm[i].Price;
                        if (rcm[i].TaxMark == 0)
                            dr["含税标志"] = "否";
                        else
                            dr["含税标志"] = "是";
                        dt.Rows.Add(dr);
                    }
                    gridControl1.DataSource = dt;
                    gridView1.Columns["id"].Visible = false;
                    gridView1.Columns["uid"].Visible = false;
                }
                else
                {
                    MessageBox.Show("查询未成功！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                FrmAddProduct fap = new FrmAddProduct();
                fap.Owner = this;
                fap.OpenType = "new";
                fap.ShowDialog();
                fap = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                FrmAddProduct fap = new FrmAddProduct();
                fap.Owner = this;
                fap.OpenType = "edit";
                fap.id = Sid;
                fap.CName = SCName;
                fap.Code = SCode;
                fap.Models = SModels;
                fap.Unit = SUnit;
                fap.TaxCode = STaxCode;
                fap.Tax = STax;
                fap.TaxMark = STaxMark;
                fap.ShowDialog();
                fap = null;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.uom.Token);
                dc.Add("id",Sid.ToString());
                RetModel rm = Share.PostData("51API/Base/GetCommidityByLikeName", dc, null);
                if (rm.Success)
                {
                    MessageBox.Show("删除成功！");
                    gridControl1.DataSource = null;
                    simpleButton1_Click(sender, e);
                    return;
                }
                else
                {
                    MessageBox.Show("删除失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                Sid = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["id"]));
                SCName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["商品名称"]).ToString();
                SCode = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["编码"]).ToString();
                SModels = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["规格型号"]).ToString();
                SUnit = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["计量单位"]).ToString();
                STaxCode = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["税收分类编码"]).ToString();
                STax = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["税率"]).ToString();
                string x = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["含税标志"]).ToString();
                if (x == "否")
                    STaxMark = 0;
                else
                    STaxMark = 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}